package com.travelsupport.ui;

import android.os.Bundle;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
import com.travelsupport.common.CustomListViewAdapter;
import com.travelsupport.controller.DbController;
import com.travelsupport.myobject.CurrencyObj;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

	public class CurrencyActivity extends Activity {
		DbController sqlHandler;
		ListView lvCustomList;
		
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_currency);
			Log.d("Phuong", "view currency");
			//for tblCurrency
			lvCustomList = (ListView) findViewById(R.id.list_currency);
			sqlHandler = new DbController(this);
			Log.d("Phuong", "create DBcontroller");
			
			if(this.isOnline())
			{
				Log.d("Phuong","isOnline1"+this.isOnline());
				showListCurrency();
			}else 
			{
				Toast.makeText(this,"Network is not available, please try again!",Toast.LENGTH_SHORT).show();
			}	

		}

		public void onClick(View v)
		{
			startActivity(new Intent(CurrencyActivity.this,SettingActivity.class));
		}
		
		public boolean isOnline() {
		    ConnectivityManager cm =
		        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		    NetworkInfo netInfo = cm.getActiveNetworkInfo();
		    if (netInfo != null && netInfo.isConnected()) {
		        return true;
		    }
		    return false;
		}
		
		private void showListCurrency()
		{
			List<CurrencyObj> currencyList = new ArrayList<CurrencyObj>();
			currencyList.clear();
			String query = "SELECT * FROM tblCurrency ";
			Cursor c1 = sqlHandler.selectQuery(query);
			Log.d("Phuong", "c1 number");
			if (c1 != null & c1.getCount() !=0)
			{
				if(c1.moveToFirst())
				{
					//do 
					//{
						CurrencyObj usdItems = new CurrencyObj();
						usdItems.set_item_currency(R.drawable.usd_flag);
						usdItems.setCurrency(c1.getFloat(c1.getColumnIndex("USD")));
						currencyList.add(usdItems);
						
						CurrencyObj uerItems = new CurrencyObj();
						uerItems.set_item_currency(R.drawable.eur_flag);
						uerItems.setCurrency(c1.getFloat(c1.getColumnIndex("EUR")));
						currencyList.add(uerItems);
						
						CurrencyObj yenItems = new CurrencyObj();
						yenItems.set_item_currency(R.drawable.yen_flag);
						yenItems.setCurrency(c1.getFloat(c1.getColumnIndex("YEN")));
						currencyList.add(yenItems);
						
						CurrencyObj rmbItems = new CurrencyObj();
						rmbItems.set_item_currency(R.drawable.rmb_flag);
						rmbItems.setCurrency(c1.getFloat(c1.getColumnIndex("RMB")));
						currencyList.add(rmbItems);
					//}while (c1.moveToNext());
				}
			}
			c1.close();
			
			CustomListViewAdapter currencyListAdapter = new CustomListViewAdapter(CurrencyActivity.this, currencyList);
			lvCustomList.setAdapter(currencyListAdapter);
			
		}

	}

